home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / Clueless.swf / scripts / Common / ResourceManager.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  7.9 KB  |  289 lines

  1. package Common
  2. {
  3.    import Common.ParticleSystem.Effect;
  4.    import flash.display.Bitmap;
  5.    import flash.display.Loader;
  6.    import flash.display.LoaderInfo;
  7.    import flash.display.MovieClip;
  8.    import flash.events.Event;
  9.    import flash.events.EventDispatcher;
  10.    import flash.events.IOErrorEvent;
  11.    import flash.events.ProgressEvent;
  12.    import flash.media.Sound;
  13.    import flash.net.URLLoader;
  14.    import flash.net.URLRequest;
  15.    
  16.    public class ResourceManager
  17.    {
  18.       
  19.       public static const C_MOVIECLIP:* = 2;
  20.       
  21.       public static const C_RES_TYPE_COUNT:* = 6;
  22.       
  23.       public static const C_TEXT:* = 3;
  24.       
  25.       public static const C_PARTICLE:* = 4;
  26.       
  27.       public static const C_SOUND:* = 1;
  28.       
  29.       public static const C_IMAGES:* = 0;
  30.       
  31.       public static const C_XML:* = 5;
  32.        
  33.       
  34.       private var loadStack:Array;
  35.       
  36.       private var sound:Array;
  37.       
  38.       public var TotalLoaded:int = 0;
  39.       
  40.       private var doneLoading:Boolean = false;
  41.       
  42.       private var particleEffects:Array;
  43.       
  44.       private var movieClip:Array;
  45.       
  46.       private var root:String;
  47.       
  48.       internal var _loaders:Array;
  49.       
  50.       private var text:Array;
  51.       
  52.       public var TotalQueued:int = 0;
  53.       
  54.       private var xmls:Array;
  55.       
  56.       private var callbackFunction:Function;
  57.       
  58.       private var images:Array;
  59.       
  60.       public function ResourceManager(param1:String = "")
  61.       {
  62.          images = [];
  63.          sound = [];
  64.          movieClip = [];
  65.          text = [];
  66.          particleEffects = [];
  67.          xmls = [];
  68.          loadStack = [];
  69.          doneLoading = false;
  70.          TotalQueued = 0;
  71.          TotalLoaded = 0;
  72.          _loaders = new Array();
  73.          super();
  74.          this.root = param1;
  75.       }
  76.       
  77.       public function setXMLResource(param1:String, param2:String) : *
  78.       {
  79.          xmls[param1] = new XML(param2);
  80.       }
  81.       
  82.       public function set Root(param1:String) : *
  83.       {
  84.          this.root = param1;
  85.       }
  86.       
  87.       public function queueResourceLoad(param1:String, param2:String, param3:int) : *
  88.       {
  89.          var _loc4_:Object = null;
  90.          if(getResource(param2,param3) != null)
  91.          {
  92.             return;
  93.          }
  94.          (_loc4_ = new Object()).filename = param1;
  95.          _loc4_.resName = param2;
  96.          _loc4_.type = param3;
  97.          loadStack.push(_loc4_);
  98.          ++TotalQueued;
  99.       }
  100.       
  101.       public function handleComplete(param1:Event) : *
  102.       {
  103.          var _loc2_:Object = null;
  104.          var _loc3_:* = undefined;
  105.          _loc2_ = Object(loadStack.shift());
  106.          ++TotalLoaded;
  107.          if(param1.target is URLLoader)
  108.          {
  109.             _loc3_ = param1.target as URLLoader;
  110.          }
  111.          else if(param1.target is Sound)
  112.          {
  113.             _loc3_ = param1.target as Sound;
  114.          }
  115.          else if(param1.target is LoaderInfo)
  116.          {
  117.             if(param1.target.content is Bitmap)
  118.             {
  119.                _loc3_ = param1.target.content as Bitmap;
  120.             }
  121.             else
  122.             {
  123.                _loc3_ = param1.target.content as MovieClip;
  124.             }
  125.          }
  126.          switch(_loc2_.type)
  127.          {
  128.             case C_IMAGES:
  129.                images[_loc2_.resName] = _loc3_;
  130.                break;
  131.             case C_SOUND:
  132.                sound[_loc2_.resName] = _loc3_;
  133.                break;
  134.             case C_MOVIECLIP:
  135.                movieClip[_loc2_.resName] = _loc3_;
  136.                break;
  137.             case C_PARTICLE:
  138.                particleEffects[_loc2_.resName] = Effect.fromXML(new XML(param1.target.data));
  139.                break;
  140.             case C_XML:
  141.                xmls[_loc2_.resName] = new XML(param1.target.data);
  142.          }
  143.          trace("Load Complete: " + _loc2_.resName);
  144.          _loc3_.removeEventListener(Event.COMPLETE,handleComplete);
  145.          loadResource();
  146.       }
  147.       
  148.       public function get DoneLoading() : *
  149.       {
  150.          return doneLoading;
  151.       }
  152.       
  153.       public function set CallbackFunction(param1:Function) : *
  154.       {
  155.          this.callbackFunction = param1;
  156.       }
  157.       
  158.       public function loadResource() : *
  159.       {
  160.          var _loc1_:* = undefined;
  161.          var _loc2_:URLRequest = null;
  162.          doneLoading = false;
  163.          if(loadStack.length == 0)
  164.          {
  165.             _loaders = new Array();
  166.             doneLoading = true;
  167.             if(callbackFunction != null)
  168.             {
  169.                callbackFunction();
  170.             }
  171.             return;
  172.          }
  173.          _loc2_ = new URLRequest((root == "" ? "" : this.root + "/") + loadStack[0].filename);
  174.          if(loadStack[0].type == C_SOUND)
  175.          {
  176.             _loc1_ = new Sound();
  177.             attachEventListener(_loc1_);
  178.          }
  179.          else if(loadStack[0].type == C_TEXT || loadStack[0].type == C_PARTICLE || loadStack[0].type == C_XML)
  180.          {
  181.             _loc1_ = new URLLoader();
  182.             attachEventListener(_loc1_);
  183.          }
  184.          else
  185.          {
  186.             _loc1_ = new Loader();
  187.             attachEventListener(_loc1_.contentLoaderInfo);
  188.          }
  189.          _loc1_.load(_loc2_);
  190.          _loaders.push(_loc1_);
  191.       }
  192.       
  193.       public function progressFunction(param1:ProgressEvent) : *
  194.       {
  195.       }
  196.       
  197.       public function getResource(param1:String, param2:uint) : *
  198.       {
  199.          switch(param2)
  200.          {
  201.             case C_IMAGES:
  202.                return images[param1];
  203.             case C_SOUND:
  204.                return sound[param1];
  205.             case C_MOVIECLIP:
  206.                return movieClip[param1];
  207.             case C_TEXT:
  208.                return text[param1];
  209.             case C_PARTICLE:
  210.                return particleEffects[param1];
  211.             case C_XML:
  212.                return xmls[param1];
  213.             default:
  214.                return;
  215.          }
  216.       }
  217.       
  218.       public function purgeAllResources() : *
  219.       {
  220.          var _loc1_:* = undefined;
  221.          _loc1_ = 0;
  222.          while(_loc1_ < C_RES_TYPE_COUNT)
  223.          {
  224.             purgeResource(_loc1_);
  225.             _loc1_++;
  226.          }
  227.       }
  228.       
  229.       public function purgeResource(param1:int) : *
  230.       {
  231.          var _loc2_:Object = null;
  232.          _loc2_ = null;
  233.          switch(param1)
  234.          {
  235.             case C_IMAGES:
  236.                for(_loc2_ in images)
  237.                {
  238.                   images[_loc2_] = null;
  239.                }
  240.                break;
  241.             case C_SOUND:
  242.                for(_loc2_ in sound)
  243.                {
  244.                   sound[_loc2_] = null;
  245.                }
  246.                break;
  247.             case C_MOVIECLIP:
  248.                for(_loc2_ in movieClip)
  249.                {
  250.                   movieClip[_loc2_] = null;
  251.                }
  252.                break;
  253.             case C_TEXT:
  254.                for(_loc2_ in text)
  255.                {
  256.                   text[_loc2_] = null;
  257.                }
  258.                break;
  259.             case C_PARTICLE:
  260.                for(_loc2_ in particleEffects)
  261.                {
  262.                   particleEffects[_loc2_] = null;
  263.                }
  264.                break;
  265.             case C_XML:
  266.                for(_loc2_ in xmls)
  267.                {
  268.                   xmls[_loc2_] = null;
  269.                }
  270.          }
  271.       }
  272.       
  273.       public function handleIOError(param1:IOErrorEvent) : void
  274.       {
  275.          trace("Load failed: IO error: " + param1.text);
  276.          loadStack.shift();
  277.          ++TotalLoaded;
  278.          loadResource();
  279.       }
  280.       
  281.       private function attachEventListener(param1:EventDispatcher) : *
  282.       {
  283.          param1.addEventListener(Event.COMPLETE,handleComplete);
  284.          param1.addEventListener(IOErrorEvent.IO_ERROR,handleIOError);
  285.          param1.addEventListener(ProgressEvent.PROGRESS,progressFunction);
  286.       }
  287.    }
  288. }
  289.